﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;

namespace zielonka.co.uk.extensions.system.data
{
    // Description: Allows you to treat an IDataReader from a database query as enumerable so that you can perform LINQ operations on it.
    /*
        using (var connection = new SqlConnection("some connection string"))
		using (var command = new SqlCommand("select * from products", connection))
		{
			connection.Open();

			using (var reader = command.ExecuteReader())
			{
				var results = reader.AsEnumerable()
					.Select(record => new Product
								{
									Name = (string)record["product_name"],
									Id = (int)record["product_id"],
									Category = (string)record["product_category"]
								})
					.GroupBy(product => product.Category);
			}
		}
    */


    // A set of extension methods for IDataReader
    public static partial class DataReaderExtensions
    {
        // Enumerates through the reads in an IDataReader.
        public static IEnumerable<IDataRecord> AsEnumerable(this IDataReader reader)
        {
            while (reader.Read())
            {
                yield return reader;
            }
        }        
    }
}
